package com.wx.spring.mvc;

import com.wx.spring.annotations.MyModelAttribute;
import com.wx.spring.annotations.MyRequstParam;
import com.wx.spring.exception.SpringMvcException;
import com.wx.spring.utils.AnnotationUtils;
import com.wx.spring.utils.VesselUtils;
import org.apache.commons.collections4.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.*;

/**
 * @ClassName Binding
 * @Author wx
 * @Description 获取注解绑定的数据
 * @Date 2019-01-30-19:19
 */
public class Binding {
    private final static Map<Class,BindingParamter> bindingParamterMap;
    static {
        bindingParamterMap  = new HashMap<>();
        bindingParamterMap.put(MyModelAttribute.class,new BindingByMyModelAttribute());
        bindingParamterMap.put(MyRequstParam.class,new BindingByMyRequstParam());
    }

    /**
     * @methodName: bingdingMethodParamters
     * @author: wx
     * @description: 运行获取绑定参数的方法
     * @param method
     * @param request
     * @date: 2019/1/30
     * @return: java.util.List<java.lang.Object>
     */
    public static List<Object> bingdingMethodParamters(Method method, HttpServletRequest request) {
        List<Object> resultParameters  = new ArrayList<>();
            Parameter[] parameters = method.getParameters();
            for (Parameter parameter : parameters) {
                if (AnnotationUtils.isNotEmpty(parameter.getAnnotations())){
                    try {
                        Object resultParameter = runBindingParamter(parameter, request);
                        if (resultParameter != null) {
                            resultParameters.add(resultParameter);
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                        throw new SpringMvcException("绑定参数异常");
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                        throw new SpringMvcException("绑定参数异常");
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                        throw new SpringMvcException("绑定参数异常");
                    }
                }
            }

        return resultParameters;
    }

    /**
     * @methodName: runBindingParamter
     * @author: wx
     * @description: 运行获取绑定参数的具体方法
     * @param parameter
     * @param request
     * @date: 2019/1/30
     * @return: java.lang.Object
     */
    private static Object runBindingParamter(Parameter parameter,HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InstantiationException {
        Set<Map.Entry<Class, BindingParamter>> entries = bindingParamterMap.entrySet();
        for (Map.Entry<Class, BindingParamter> entry : entries) {
            if(AnnotationUtils.isNotEmpty(parameter.getAnnotation(entry.getKey()))){
                return entry.getValue().getBindingParamter(parameter,request);
            }
        }
        return null;
    }

}
